package com.znsd.zl.dict.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.znsd.zl.utils.PageUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @ClassName ConditionPageUtils
 * @Description TODO
 * @Author zhubin
 * @Date 2020/8/18 9:50
 * @Version v1.0
 */
public class ConditionPageUtils {

	public static PageUtils conditionPage(Map<String,Object> condition, BaseMapper baseMapper){
		Object current = condition.remove("page");
		Object size = condition.remove("limit");
		// 默认分页
		Integer page = 1;
		Integer limit = 10;
		if (current != null) {
			page = Integer.parseInt(current.toString());
		}
		if (size != null) {
			limit = Integer.parseInt(size.toString());
		}
		// 多条件查询封装
		QueryWrapper qw = new QueryWrapper<>();
		//循环封装条件
		for (String key : condition.keySet()) {
			Object value =  condition.get(key);
			if ("t".equals(key)) { continue; }
			qw.eq(!StringUtils.isEmpty(value),key,value);
		}
		IPage iPage = baseMapper.selectPage(new Page<>(page, limit), qw);
		return new PageUtils(iPage);
	}

}
